class Solution {
public:
    vector<int> c;
    int lowBit(int x){
        return x & (-x);
    }
    int query(int p){
        int ret = 0;
        while(p > 0){
            ret += c[p];
            p -= lowBit(p);
        }
        return ret;
    }
    void update(int p,int u){
        while(p < c.size()){
            c[p] += u;
            p += lowBit(p);
        }
    }
    
    vector<int> processQueries(vector<int>& queries, int m) {
        int n = queries.size();
        vector<int> ret(n);
        c.resize(m+n+1);
        
        vector<int> pos(m+1);
        for(int i = 1;i <= m;i++){
            pos[i] = i + n;
            update(n+i,1);
        }
        
        for(int i = 0;i < n;i++){
            int& id = pos[queries[i]];
            update(id,-1);
            ret[i] = query(id);
            id = n - i;
            update(id,1);
        }
        
        return ret;
    }
};
